﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.IO;
using Microsoft.Win32;
using DataSimDLL;

namespace BDSim
{
    /// <summary>
    /// LoadUserTrackView.xaml 的交互逻辑
    /// </summary>
    public partial class LoadUserTrackView : UserControl, INotifyPropertyChanged, ILayoutContent
    {
        public LoadUserTrackView()
        {
            InitializeComponent();
        }

        #region //属性
        //用户模型名字
        private string _UserModelName = "";

        public string UserModelName
        {
            get { return _UserModelName; }
            set { _UserModelName = value; NotifyPropertyChanged("UserModelName"); }
        }

        //用户在界面选中的行字符串
        string _SelectedItem = "";

        public string SelectedItem
        {
            get { return _SelectedItem; }
            set { _SelectedItem = value; NotifyPropertyChanged("SelectedItem"); GetSelectedItemDetail(value); }
        }

        //导入文件的轨迹集合
        ObservableCollection<string> _TrackDataList = new ObservableCollection<string>();

        public ObservableCollection<string> TrackDataList
        {
            get { return _TrackDataList; }
            set { _TrackDataList = value; NotifyPropertyChanged("TrackDataList"); }
        }

       
        //是否导入的是XYZ坐标
        bool _IsXYZ = true;

        public bool IsXYZ
        {
            get { return _IsXYZ; }
            set { _IsXYZ = value; NotifyPropertyChanged("IsXYZ"); }
        }

        //是否导入的BLH坐标
        bool _IsBLH = false;

        public bool IsBLH
        {
            get { return _IsBLH; }
            set { _IsBLH = value; NotifyPropertyChanged("IsBLH"); }
        }

        //用户选中的行按空格或分号分割开的字符串数组
        ObservableCollection<string> _ColunmList = new ObservableCollection<string>();

        public ObservableCollection<string> ColunmList
        {
            get { return _ColunmList; }
            set { _ColunmList = value; NotifyPropertyChanged("ColunmList"); }
        }

        //用户选中的XYZ坐标的列索引
        int[] _XYZ_LableIndex = new int[3]{-1,-1,-1};

        public int[] XYZ_LableIndex
        {
            get { return _XYZ_LableIndex; }
            set { _XYZ_LableIndex = value; NotifyPropertyChanged("XYZ_LableIndex"); }
        }

        //用户选中的BLH坐标的列的索引
        int[] _BLH_LableIndex = new int[3] { -1, -1, -1 };

        public int[] BLH_LableIndex
        {
            get { return _BLH_LableIndex; }
            set { _BLH_LableIndex = value; NotifyPropertyChanged("BLH_LableIndex"); }
        }

        //用户导入的轨迹数据的时间间隔
        double _TrackSamplingTimespan = 60;
        /// <summary>
        /// 轨迹文件数据采样间隔
        /// </summary>
        public double TrackSamplingTimespan
        {
            get { return _TrackSamplingTimespan; }
            set { _TrackSamplingTimespan = value; }
        }

        #endregion
        
        #region //成员

        //用户设备uid
        public Guid UserUid; 

        #endregion

        #region //公开方法
        public void Init(Guid uid)
        {
            UserUid = uid;
        }

        public void ReleaseResources()
        {

        }
        #endregion

        #region //私有方法
       /// <summary>
        /// 获取用户选中的行按空格或分号分割开的字符串数组
       /// </summary>
       /// <param name="str"></param>
        void GetSelectedItemDetail(string str)
        {

            ColunmList.Clear();
            if (str != null)
            {
                string[] splitStringList = str.Split(new char[2] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var item in splitStringList)
                {
                    ColunmList.Add(item);
                }
            }

        }

        /// <summary>
        /// 打开轨迹文件按钮的响应函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenTrackFile_Click(object sender, RoutedEventArgs e)
        {
            //  进行当前路径保存
            string currentDirectory = Directory.GetCurrentDirectory();
            OpenFileDialog OpenFile = new OpenFileDialog();
            OpenFile.Title = "选择轨迹文件";
            OpenFile.InitialDirectory = Directory.GetCurrentDirectory();
            //OpenFile.Filter = "文本文档(*.txt)|*.txt";
            if (OpenFile.ShowDialog() == true)
            {
                TrackDataList.Clear();
                textBox1.Text = OpenFile.FileName;
                string[] strArray = File.ReadAllLines(OpenFile.FileName);
                foreach (var item in strArray)
                {
                    TrackDataList.Add(item);
                }
            }

            //  还原路径
            Directory.SetCurrentDirectory(currentDirectory);
        }

        /// <summary>
        /// 应用轨迹文件按钮的响应函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ApplyTrackData_Click(object sender, RoutedEventArgs e)
        {
            //是否正确导入XYZ坐标的标志
            bool flagXYZ = (XYZ_LableIndex[0] >= 0) && (XYZ_LableIndex[1] >= 0) && (XYZ_LableIndex[2] >= 0);
            //是否正确导入BLH的标志
            bool flagBLH = (BLH_LableIndex[0] >= 0) && (BLH_LableIndex[1] >= 0) && (BLH_LableIndex[2] >= 0);
            //如果导入文件的界面操作方式都正常
            if (TrackSamplingTimespan > 0 && (flagXYZ || flagBLH))
            {
                int line = -1;
                int SelectLineIndex = TrackDataListView.SelectedIndex;

                //清空该用户设备的数据列表
                (this.DataContext as DynamicUser).LoadedTrackData.Clear();
                //将此设备的轨迹导入方式标记为文件导入
                (this.DataContext as DynamicUser).TrackGenType = ETrackGenType.Import;
                (this.DataContext as DynamicUser).LoadedTrackSampleTimespan = TrackSamplingTimespan;

                //如果导入的是XYZ坐标
                if (IsXYZ)
                {
                    try
                    {


                        //向用户设备的轨迹属性中加入数据
                        for (int i = SelectLineIndex; i < TrackDataList.Count; i++)
                        {
                            line = i;
                            NodePosVel xyz = new NodePosVel();
                            string[] splitStringList = TrackDataList[i].Split(new char[2] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                            xyz.X = Convert.ToDouble(splitStringList[XYZ_LableIndex[0]]);
                            xyz.Y = Convert.ToDouble(splitStringList[XYZ_LableIndex[1]]);
                            xyz.Z = Convert.ToDouble(splitStringList[XYZ_LableIndex[2]]);
                            (this.DataContext as DynamicUser).LoadedTrackData.Add(xyz);                         
                        }

                        MessageBox.Show("已成功导入" + (1 + line - SelectLineIndex).ToString() + "行轨迹数据!");
                        if ((this.DataContext as DynamicUser).LoadedTrackData.Count < 10)
                        {
                            MessageBox.Show("用户导入的有效数据组数小于10个，接收机坐标默认为用户所选择第一行数据为静态坐标！");
                        }
                    }
                    catch
                    {
                        //当导入数据有误时，自动退出导入操作，并提示导入成功的数据条目
                        MessageBox.Show("已成功导入" + (line - SelectLineIndex).ToString() + "行轨迹数据!");
                    }
                }
                //如果导入的是BLH坐标
                else if (IsBLH)
                {
                    try
                    {
                        for (int i = SelectLineIndex; i < TrackDataList.Count; i++)
                        {
                            line = i;
                            BLH_PosVel blh = new BLH_PosVel();
                            string[] splitStringList = TrackDataList[i].Split(new char[2] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                            blh.B = Convert.ToDouble(splitStringList[BLH_LableIndex[0]]);
                            blh.L = Convert.ToDouble(splitStringList[BLH_LableIndex[1]]);
                            blh.H = Convert.ToDouble(splitStringList[BLH_LableIndex[2]]);
                            //将BLH转化为XYZ
                            NodePosVel xyz;
                            TimeSpaceDll.BLH2XYZ(blh, ECFtype.type_CGCS2000, out xyz);
                            (this.DataContext as DynamicUser).LoadedTrackData.Add(xyz);                        
                        }

                        MessageBox.Show("已成功导入" + (line-SelectLineIndex).ToString() + "行轨迹数据!");
                        if ((this.DataContext as DynamicUser).LoadedTrackData.Count < 10)
                        {
                            MessageBox.Show("用户导入的有效数据组数小于10个，接收机坐标默认为用户所选择第一行数据为静态坐标！");
                        }
                    }
                    catch
                    {
                        //当导入数据有误时，自动退出导入操作，并提示导入成功的数据条目
                        MessageBox.Show("已成功导入" + (line-SelectLineIndex).ToString() + "行轨迹数据!");
                    }
                }
            }
            else
            {
                MessageBox.Show("请输入正确的采样间隔和轨迹坐标");
            }

        }

        #endregion

        #region //事件

        //属性改变通知时间
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        public void ReleaseData()
        {
            //throw new NotImplementedException();
        }
    }
}
